import android.content.Intent;
import android.os.Bundle;
import android.widget.EditText;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import com.example.rate.R;

public class EditRateActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_edit_rate);

        // 获取当前汇率
        Intent intent = getIntent();
        double usd = intent.getDoubleExtra("USD_RATE", 6.5);
        double eur = intent.getDoubleExtra("EUR_RATE", 7.5);
        double jpy = intent.getDoubleExtra("JPY_RATE", 0.06);

        // 初始化输入框
        EditText etUsd = findViewById(R.id.et_usd_rate);
        EditText etEur = findViewById(R.id.et_eur_rate);
        EditText etJpy = findViewById(R.id.et_jpy_rate);
        etUsd.setText(String.valueOf(usd));
        etEur.setText(String.valueOf(eur));
        etJpy.setText(String.valueOf(jpy));

        // 保存按钮点击
        findViewById(R.id.btn_save).setOnClickListener(v -> {
            try {
                Intent result = new Intent();
                result.putExtra("NEW_USD_RATE", Double.parseDouble(etUsd.getText().toString()));
                result.putExtra("NEW_EUR_RATE", Double.parseDouble(etEur.getText().toString()));
                result.putExtra("NEW_JPY_RATE", Double.parseDouble(etJpy.getText().toString()));
                setResult(RESULT_OK, result);
                finish();
            } catch (NumberFormatException e) {
                Toast.makeText(this, "请输入有效数字", Toast.LENGTH_SHORT).show();
            }
        });
    }
}